
/*************************************************************************
;    Copyright (c) 2004-2010, fhbit                      
;    All rights reserved.                                                             
;     
;    file name : DRIVER_MAX6692.c
;    description: the function file of max6692 driver   
;
;    History     :
;     1.  created : charles liu, 2010-02-25
;*************************************************************************/

#include    "lpc_data_type.h"
#include "i2c_module_interface.h"
#include    "driver_max6692.h"

/*******************************************/
extern void Delay(LPC_INT32U dly);

LPC_INT8U Read_Max6692Manufacture(void)
{
	LPC_INT8U sndBuf[2];
	LPC_INT8U rcvBuf[2];

	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_MANUFACTURE_ID;
	I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2);

	return rcvBuf[1];
}

LPC_INT8U Read_Max6692Revision(void)
{
	LPC_INT8U sndBuf[2];
	LPC_INT8U rcvBuf[2];    

	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_REVISION_ID;
	I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2);

	return rcvBuf[1];   
}

LPC_STATUS Init_Max6692Confg(void)
{
	LPC_INT8U sndBuf[3];

	if(Read_Max6692Manufacture()!=0x4d)
	{
		return LPC_FAILURE;
	}
	if(Read_Max6692Revision()!=0x59)
	{
		return LPC_FAILURE;
	}

	sndBuf[0] = MAX6692;
	sndBuf[1] = WR_CONFG_BYTE;
	sndBuf[2] =  0x40;            //set to standby mode
	return I2C_TxData( I2C_CHANNEL_LOCAL, 3, &sndBuf[0]);   


}

LPC_STATUS Send_OneShot(void)
{
	LPC_INT8U sndBuf[2];

	sndBuf[0] = MAX6692;
	sndBuf[1] = WR_COMMD_ONESHOT;
	return I2C_TxData( I2C_CHANNEL_LOCAL, 2, &sndBuf[0]); 
}

LPC_STATUS Read_Max6692Temperature(LPC_INT8U *buf)
{
	LPC_INT8U status,count;
	LPC_INT8U temperature;
	LPC_INT8U sndBuf[2];
	LPC_INT8U rcvBuf[2];

	count = 0;
	status = 0;    
	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_STATE_BYTE;
	I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2);
	status = rcvBuf[1];
	while(status&0x80)
	{
		count++;
		if(count>1000)
		{            
			break;
		}
		Delay(10);
		if(2 != I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2))
		{
			return LPC_FAILURE;
		}
		status = rcvBuf[1];
	}
	if(count>=1000)
	{
		return LPC_FAILURE;
	}

	temperature = 0;
	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_LOCAL_INTEGER;
	if(2 != I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2))
	{
		return LPC_FAILURE;
	}
	temperature = rcvBuf[1];

	*buf = temperature;

	temperature = 0;
	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_LOCAL_DECIMAL;
	if(2 != I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2))
	{
		return LPC_FAILURE;
	}
	temperature = rcvBuf[1];

	*(buf+1) = temperature;

	temperature = 0;
	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_REMOT_INTEGER;
	if(2 != I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2))
	{
		return LPC_FAILURE;
	}
	temperature = rcvBuf[1];

	*(buf+2) = temperature;

	temperature = 0;
	sndBuf[0] = MAX6692;
	sndBuf[1] = RD_REMOT_DECIMAL;
	if(2 != I2c_Device_Read ( I2C_CHANNEL_LOCAL,&sndBuf[0],2,&rcvBuf[0],2))
	{
		return LPC_FAILURE;
	}
	temperature = rcvBuf[1];

	*(buf+3) = temperature;

	return LPC_SUCCESS;

}

